import {relations} from 'drizzle-orm/relations'
import {sysUser, sysLoginLog, sysReqLog, sysUserRole, sysRole, sysRoleMenu, sysMenu} from './schema'

export const sysLoginLogRelations = relations(sysLoginLog, ({one}) => ({
  sysUser: one(sysUser, {
    fields: [sysLoginLog.userId],
    references: [sysUser.id],
  }),
}))

export const sysReqLogRelations = relations(sysReqLog, ({one}) => ({
  sysUser: one(sysUser, {
    fields: [sysReqLog.userId],
    references: [sysUser.id],
  }),
}))

export const sysUserRelations = relations(sysUser, ({many}) => ({
  sysLoginLogs: many(sysLoginLog),
  sysReqLogs: many(sysReqLog),
  sysUserRoles: many(sysUserRole),
}))

export const sysUserRoleRelations = relations(sysUserRole, ({one}) => ({
  sysRole: one(sysRole, {
    fields: [sysUserRole.roleId],
    references: [sysRole.id],
  }),
  sysUser: one(sysUser, {
    fields: [sysUserRole.userId],
    references: [sysUser.id],
  }),
}))

export const sysRoleRelations = relations(sysRole, ({many}) => ({
  sysRoleMenus: many(sysRoleMenu),
  sysUserRoles: many(sysUserRole),
}))

export const sysRoleMenuRelations = relations(sysRoleMenu, ({one}) => ({
  sysMenu: one(sysMenu, {
    fields: [sysRoleMenu.menuId],
    references: [sysMenu.id],
  }),
  sysRole: one(sysRole, {
    fields: [sysRoleMenu.roleId],
    references: [sysRole.id],
  }),
}))

export const sysMenuRelations = relations(sysMenu, ({many}) => ({
  sysRoleMenus: many(sysRoleMenu),
}))
